﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：InvoiceCheck.aspx.cs
    // 文件功能描述：发票审核，批量审核
    // 创建标识：吕海乐2009-04-30
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class InvoiceCheck : BasePage
    {
        public InvoiceCheck()
        {
            this.ModuleNo = Module.发票管理.ToString();
        }

        IBLL_Purchase ip = Factory.BLL_Purchase();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
                if (gvwInvoice.Rows.Count <= 0)
                {
                    btnConfirm.Visible = false;
                }
            }
        }

        /// <summary>
        /// 绑定发票信息
        /// </summary>
        private void SetBind()
        {
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;

            //构造查询实体
            Model.Query.Invoice queryModel = new Model.Query.Invoice();
            queryModel.AuditStatus = 0;
            queryModel.SortFields = "InvoiceTime DESC";
            //加入地区过滤条件，管理机构只能管理本地区下的采购单
            queryModel.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;

            IList<Model.View.Invoice> invoiceList = ip.GetInvoiceByWhere(queryModel , AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwInvoice.DataSource = invoiceList;
            gvwInvoice.DataBind();
        }

        /// <summary>
        /// 操作  
        /// </summary>
        /// <param name="type">操作的类型，0是不通过，1是通过</param>
        /// <param name="reason">不通过的原因，通过则reason为空</param>
        private void Operate(string type, string reason)
        {
            List<Invoice> invoiceList = new List<Invoice>();
            int count = 0;//需操作的发票个数
            //获取操作的发票信息
            for (int i = 0; i < gvwInvoice.Rows.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)gvwInvoice.Rows[i].FindControl("chkInvoice");
                if (chk.Checked == true)
                {
                    Invoice invoice = new Invoice();
                    invoice.InvoiceSerialID = chk.Value;
                    invoice.AuditPerson = CurrentAccount.GetUser().UserID;
                    if (type == "0") { invoice.NoPassReason = reason; }
                    invoiceList.Add(invoice);
                    count++;
                }
            }

            //根据不同的类型进行不同的操作
            //不通过
            if (count > 0)
            {
                if (type == "0")
                {
                    if (ip.NoConfirmInvoice_Batch(invoiceList) == 1)
                    {
                        SetBind();
                        txtReason.Text = "";
                    }
                    else
                    {
                        HZ.Web.WebUtil.Alert("操作失败！",this);
                    }
                }
                //通过
                else if (type == "1")
                {
                    string[] a=new string[0];
                    if (ip.ConfirmInvoice_Batch(invoiceList, a) == 1)
                    {
                        SetBind();
                        txtReason.Text = "";
                    }
                    else
                    {
                        HZ.Web.WebUtil.Alert("操作失败！",this);
                    }
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何要操作的发票！",this);
            }

        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            if (ddlType.SelectedItem.Value == "1")
            {
                Operate("1", "");
            }
            else
            {
                if (!string.IsNullOrEmpty(txtReason.Text.Trim()))
                {
                    string reason = txtReason.Text;
                    Operate("0", reason);
                }
                else
                {
                    HZ.Web.WebUtil.Alert("不通过原因不能为空！",this);
                    txtReason.Focus();
                }
            }
        }

        /// <summary>
        /// 绑定发票的状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)e.Row.FindControl("chkInvoice");
                Literal ltr = e.Row.FindControl("ltrStatus") as Literal;
                if (ltr.Text == "0")
                {
                    ltr.Text = "未审核";
                }
                else if (ltr.Text == "1")
                {
                    ltr.Text = "已通过";
                    chk.Disabled = true;
                }
                else if (ltr.Text == "2")
                {
                    ltr.Text = "未通过";
                    chk.Disabled = true;
                }
            }
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            SetBind();
        }
    }
}
